package com.socialnetspace.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import javax.persistence.EntityManager;

import org.springframework.transaction.annotation.Transactional;

public abstract class BaseDAO 
{

	protected EntityManager entityManager;
	
	protected HashMap<String,Long>  eventIdMap = new HashMap<String,Long>();
	
	
	public void setEntityManager(EntityManager entityManager) {
		this. entityManager = entityManager;
	}
	
	
	public <T> T getById(final Class<T> clazz, final Serializable id) {
		T object = clazz.cast(entityManager.find(clazz, id));
		return object;
	}
	
	
	@Transactional
	public Object save(Object ob) {
		Object object = entityManager.merge(ob);
		return object;
	}

	
	@Transactional
	public void persist(Object ob) {
		entityManager.persist(ob);
	}
	
	
	@SuppressWarnings("unchecked")
	public <T>  ArrayList<T> getAll(final Class<T> clazz) {
		String hqlQuery = "from "+ clazz.getSimpleName();
		ArrayList<T> list  = (ArrayList<T>)entityManager.createQuery(hqlQuery).getResultList();
		return list;
	}


}